package com.hu.zhcc.shiro.controller.xsf;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.xsf.dto.TMealsDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.TVaccinNoteDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.TMeals;
import com.hu.zhcc.shiro.entity.xsf.pojo.TVaccinNote;
import com.hu.zhcc.shiro.entity.xsf.vo.TMealsVO;
import com.hu.zhcc.shiro.entity.xsf.vo.TVaccinNoteVO;
import com.hu.zhcc.shiro.service.xsf.TVaccinNoteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.jar.JarOutputStream;

@RestController
@RequestMapping("/tVaccinNote")
public class TVaccinNoteController {
    @Autowired
    private TVaccinNoteService tVaccinNoteService;

    @GetMapping("")
    public ResponseEntity<Page<TVaccinNoteVO>> getRoleList(@RequestParam(required = false) String name,
                                                           int offset, int limit) {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            paremeters.put("name", name);
        }
        Page<TVaccinNoteDTO> result = this.tVaccinNoteService.queryAllTVaccinNote(paremeters, offset, limit);
        List<TVaccinNoteVO> voList = new ArrayList<TVaccinNoteVO>();
        if( result.getRows()!=null ){

            for(TVaccinNoteDTO dto : result.getRows()) {
                TVaccinNoteVO vo = new TVaccinNoteVO();
                BeanUtils.copyProperties(dto, vo);
                voList.add(vo);
            }
            return ResponseEntity.ok(new Page<TVaccinNoteVO>(result.getTotal(), voList));
        }else {
            return null;
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<TVaccinNoteVO> getById(@PathVariable("id") int id) {
        TVaccinNoteDTO dto = this.tVaccinNoteService.getById(id);
        if(dto == null) {
            return ResponseEntity.notFound().build();
        }

        TVaccinNoteVO vo = new TVaccinNoteVO();
        BeanUtils.copyProperties(dto, vo);
        return ResponseEntity.ok(vo);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateTEvaluate(@PathVariable("id") int id, @RequestBody TVaccinNote vo) {
//        System.out.println(vo);
        int rows = this.tVaccinNoteService.updateTVaccinNote(vo);
        return rows == 0 ? ResponseEntity.notFound().build() :
                ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<?> removeTEvaluate(@PathVariable("id") int id) {
        return this.tVaccinNoteService.removeTVaccinNote(id) > 0 ?
                ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }

    @PostMapping("")
    public ResponseEntity<TVaccinNoteVO> saveTEvaluate(@RequestBody TVaccinNoteVO vo) {
        TVaccinNoteDTO dto = new TVaccinNoteDTO();
        BeanUtils.copyProperties(vo, dto);
        this.tVaccinNoteService.saveTVaccinNote(dto);
        vo.setId(dto.getId());
        return ResponseEntity.status(HttpStatus.CREATED).body(vo);
    }
}
